package com.atguigu.juc.day02.rw;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Demo04WriteToRead {

    public static void main(String[] args) {

        ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

        Lock readLock = readWriteLock.readLock();
        Lock writeLock = readWriteLock.writeLock();

        // 先加写锁
        writeLock.lock();
        System.out.println("加写锁成功。");

        // 在写锁尚未释放时加读锁
        readLock.lock();
        System.out.println("加读锁成功。");

        // 操作完成，读锁释放
        readLock.unlock();

        // 操作完成，写锁释放
        writeLock.unlock();

    }
}
